package com.youi.util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * 文件上传工具类
 * @author erdong.Chan
 *
 */
public class FileUploadUtil {
	
	public static String currentPojectUrl=null;/**本地工程根路径*/
	
	static {
		if (currentPojectUrl==null) {
			currentPojectUrl=new File(new File(DateUtil.class.getResource("/").getPath()).getParent()).getParent()+File.separator;//两个上级
			System.err.println(currentPojectUrl);
		}
	}
	
	/**
	 * 文件上传
	 * @param fileUrl
	 * @param fileName
	 * @param fileArr
	 * @return
	 * @throws IOException
	 */
	public static String fileUpload(String fileUrl,String fileName,byte [] fileArr) throws IOException{
		String fileType = fileName.substring(fileName.lastIndexOf(".")+1);
		String formatYear=new SimpleDateFormat("yyyy").format(new Date());
		String formatDate=new SimpleDateFormat("MMdd").format(new Date());
		String formatTime=new SimpleDateFormat("HHmmss").format(new Date());
		fileName=formatTime+getRandom()+"."+fileType;
		String severUrl="upload"+File.separator+formatYear+File.separator+formatDate+File.separator+fileName;//服务器的相对地址
		String localUrl=fileUrl+severUrl;//保存在计算机上的地址
		File file =new File(localUrl);
		if(!file.exists()){
			if(!file.getParentFile().exists()){
				file.getParentFile().mkdirs();
			}
			file.createNewFile();
		}
		FileCopyUtils.copy(fileArr, file);
		/**如果是图片 则压缩*/
		if (checkFileType(fileName)) {
			compressImage(new FileInputStream(file), 0, 0, localUrl);
		}
		return  "upload/"+formatYear+"/"+formatDate+"/"+fileName;
	}
	
	
	/**
	 * 文件上传返回保存的地址
	 * @param fileUrl
	 * @param fileName
	 * @param fileArr
	 * @return
	 * @throws IOException
	 */
	public static String fileUploadReturnLocalUrl(String fileUrl,String fileName,byte [] fileArr) throws IOException{
		String fileType = fileName.substring(fileName.lastIndexOf(".")+1);
		String formatYear=new SimpleDateFormat("yyyy").format(new Date());
		String formatDate=new SimpleDateFormat("MMdd").format(new Date());
		String formatTime=new SimpleDateFormat("HHmmss").format(new Date());
		fileName=formatTime+getRandom()+"."+fileType;
		String severUrl="upload"+File.separator+formatYear+File.separator+formatDate+File.separator+fileName;//服务器的相对地址
		String localUrl=fileUrl+severUrl;//保存在计算机上的地址
		File file =new File(localUrl);
		if(!file.exists()){
			if(!file.getParentFile().exists()){
				file.getParentFile().mkdirs();
			}
			file.createNewFile();
		}
		FileCopyUtils.copy(fileArr, file);
		/**如果是图片 则压缩*/
		if (checkFileType(fileName)) {
			compressImage(new FileInputStream(file), 0, 0, localUrl);
		}
		return  localUrl;
	}
	
	/**
	 * 原文件上传
	 * @param fileUrl
	 * @param fileName
	 * @param fileArr
	 * @return
	 * @throws IOException
	 */
	public static String realFileUpload(String fileUrl,String fileName,byte [] fileArr) throws IOException{
		String fileType = fileName.substring(fileName.lastIndexOf(".")+1);
		String formatYear=new SimpleDateFormat("yyyy").format(new Date());
		String formatDate=new SimpleDateFormat("MMdd").format(new Date());
		String formatTime=new SimpleDateFormat("HHmmss").format(new Date());
		fileName="r"+formatTime+getRandom()+"."+fileType;
		String severUrl="upload"+File.separator+formatYear+File.separator+formatDate+File.separator+fileName;//服务器的相对地址
		String localUrl=fileUrl+severUrl;//保存在计算机上的地址
		File file =new File(localUrl);
		if(!file.exists()){
			if(!file.getParentFile().exists()){
				file.getParentFile().mkdirs();
			}
			file.createNewFile();
		}
		FileCopyUtils.copy(fileArr, file);
		/**如果是图片 则压缩*/
		if (checkFileType(fileName)) {
			compressImage(new FileInputStream(file), 0, 0,localUrl);
		}
		return  "upload/"+formatYear+"/"+formatDate+"/"+fileName;
	}
	
	/**
	 * 原文件上传(带水印)
	 * @param fileUrl
	 * @param fileName
	 * @param fileArr
	 * @return
	 * @throws IOException
	 */
	public static String realFileUploadMark(String fileUrl,String fileName,byte [] fileArr) throws IOException{
		String fileType = fileName.substring(fileName.lastIndexOf(".")+1);
		String formatYear=new SimpleDateFormat("yyyy").format(new Date());
		String formatDate=new SimpleDateFormat("MMdd").format(new Date());
		String formatTime=new SimpleDateFormat("HHmmss").format(new Date());
		fileName="r"+formatTime+getRandom()+"."+fileType;
		String severUrl="upload"+File.separator+formatYear+File.separator+formatDate+File.separator+fileName;//服务器的相对地址
		String localUrl=fileUrl+severUrl;//保存在计算机上的地址
		File file =new File(localUrl);
		if(!file.exists()){
			if(!file.getParentFile().exists()){
				file.getParentFile().mkdirs();
			}
			file.createNewFile();
		}
		FileCopyUtils.copy(fileArr, file);
		/**如果是图片 则压缩*/
		if (checkFileType(fileName)) {
			compressImage(new FileInputStream(file), 0, 0,localUrl);
		}
		markImage(fileUrl, localUrl);//添加水印
		return  "upload/"+formatYear+"/"+formatDate+"/"+fileName;
	}
	
	
	/**
	 * 文件上传  返回路径和大小
	 * @param modelMap
	 * @param fileUrl
	 * @param fileName
	 * @param fileArr
	 * @throws IOException
	 */
	public static void fileUpload(ModelMap modelMap,String fileUrl,String fileName,byte [] fileArr) throws IOException{
		String fileType = fileName.substring(fileName.lastIndexOf(".")+1);;
		String formatYear=new SimpleDateFormat("yyyy").format(new Date());
		String formatDate=new SimpleDateFormat("MMdd").format(new Date());
		String formatTime=new SimpleDateFormat("HHmmss").format(new Date());
		fileName=formatTime+getRandom()+"."+fileType;
		String severUrl="upload"+File.separator+formatYear+File.separator+formatDate+File.separator+fileName;//服务器的相对地址
		String localUrl=fileUrl+severUrl;//保存在计算机上的地址
		File file =new File(localUrl);
		if(!file.exists()){
			if(!file.getParentFile().exists()){
				file.getParentFile().mkdirs();
			}
			file.createNewFile();
		}
		FileCopyUtils.copy(fileArr, file);
		/**如果是图片 则压缩*/
		if (checkFileType(fileName)) {
			compressImage(new FileInputStream(file), 0, 0,localUrl);
		}else if (fileName.contains(".amr")||fileName.contains(".AMR")) {  /**如果是amr*/
			modelMap.put("size", file.length());
			modelMap.put("time", getAmrDuration(file)/1000);
		}else { /**音视频返回长度和时长*/
			modelMap.put("size", file.length());
//			modelMap.put("time", getDuration(file));
		}
		modelMap.put("url", "upload/"+formatYear+"/"+formatDate+"/"+fileName);
	}
	
	/**
	 * 文件上传  返回路径和大小
	 * @param modelMap
	 * @param fileUrl
	 * @param fileName
	 * @param fileArr
	 * @throws IOException
	 */
	public static ModelMap fileUploadReturnMap(String fileUrl,String fileName,byte [] fileArr) throws IOException{
		String fileType = fileName.substring(fileName.lastIndexOf(".")+1);;
		String formatYear=new SimpleDateFormat("yyyy").format(new Date());
		String formatDate=new SimpleDateFormat("MMdd").format(new Date());
		String formatTime=new SimpleDateFormat("HHmmss").format(new Date());
		fileName=formatTime+getRandom()+"."+fileType;
		String severUrl="upload"+File.separator+"file"+File.separator+formatYear+File.separator+formatDate+File.separator+fileName;//服务器的相对地址
		String localUrl=fileUrl+severUrl;//保存在计算机上的地址
		File file =new File(localUrl);
		if(!file.exists()){
			if(!file.getParentFile().exists()){
				file.getParentFile().mkdirs();
			}
			file.createNewFile();
		}
		ModelMap modelMap=new ModelMap();
		/**如果是图片 则压缩*/
		if (checkFileType(fileName)) {
			compressImage(new FileInputStream(file), 0, 0,localUrl);
		}else if (fileName.contains(".amr")||fileName.contains(".AMR")) {  /**如果是amr*/
			modelMap.put("size", file.length());
			modelMap.put("time", getAmrDuration(file)/1000);
		}else { /**音视频返回长度和时长*/
			modelMap.put("size", file.length());
//			modelMap.put("time", getDuration(file));
		}
		modelMap.put("url", "upload/file/"+formatYear+"/"+formatDate+"/"+fileName);
		return modelMap;
	}
	
	/**
	 * 图片压缩上传 --io流
	 * @param fileUrl
	 * @param fileName
	 * @param fileStream
	 * @param width
	 * @param height
	 * @return
	 * @throws IOException
	 */
	public static String imgUpload(String fileUrl,String fileName,InputStream fileStream,Integer width,Integer height,Integer comRate,Integer mark) throws IOException{
		String fileType = fileName.substring(fileName.lastIndexOf(".")+1);;
		String formatYear=new SimpleDateFormat("yyyy").format(new Date());
		String formatDate=new SimpleDateFormat("MMdd").format(new Date());
		String formatTime=new SimpleDateFormat("HHmmss").format(new Date());
		fileName=formatTime+getRandom()+"."+fileType;
		String severUrl="upload"+File.separator+formatYear+File.separator+formatDate+File.separator+fileName;//服务器的相对地址
		String localUrl=fileUrl+severUrl;//保存在计算机上的地址
		File file =new File(localUrl);
		if(!file.exists()){
			if(!file.getParentFile().exists()){
				file.getParentFile().mkdirs();
			}
			file.createNewFile();
		}
		if (width!=null&&height!=null) {
			compressImage(fileStream, width, height, comRate, localUrl);
			compressImage(new FileInputStream(file), 0, 0,localUrl);
		}else {
			compressImage(fileStream, 0, 0, localUrl);
		}
		
		/**添加水印*/
		if (mark!=null) {
			//markImage(fileUrl, localUrl);
		}
		return "upload/"+formatYear+"/"+formatDate+"/"+fileName; //服务器的相对地址
	}
	
	/**
	 * 图片压缩上传  --数组
	 * @param fileUrl
	 * @param fileName
	 * @param fileStream
	 * @param width
	 * @param height
	 * @return
	 * @throws IOException
	 */
	public static String imgUpload(String fileUrl,String fileName,byte[] bytes,Integer width,Integer height,Integer comRate,Integer mark) throws IOException{
		String fileType = fileName.substring(fileName.lastIndexOf(".")+1);;
		String formatYear=new SimpleDateFormat("yyyy").format(new Date());
		String formatDate=new SimpleDateFormat("MMdd").format(new Date());
		String formatTime=new SimpleDateFormat("HHmmss").format(new Date());
		fileName=formatTime+getRandom()+"."+fileType;
		String severUrl="upload"+File.separator+"image"+File.separator+formatYear+File.separator+formatDate+File.separator+fileName;//服务器的相对地址
		String localUrl=fileUrl+severUrl;//保存在计算机上的地址
		File file =new File(localUrl);
		if(!file.exists()){
			if(!file.getParentFile().exists()){
				file.getParentFile().mkdirs();
			}
			file.createNewFile();
		}
		
		ByteArrayInputStream fileStream =new ByteArrayInputStream(bytes);
		if (width!=null&&height!=null) {
			compressImage(fileStream, width, height, comRate, localUrl);
			compressImage(new FileInputStream(file), 0, 0,localUrl);
		}else {
			compressImage(fileStream, 0, 0, localUrl);
		}
		
		/**添加水印*/
		if (mark!=null) {
			//markImage(fileUrl, localUrl);
		}
		return "upload/image/"+formatYear+"/"+formatDate+"/"+fileName; //服务器的相对地址
	}
	
	/**
	 * Base64图片上传
	 * @param fileUrl
	 * @param fileName
	 * @param fileArr
	 * @return
	 * @throws IOException
	 */
	public static String fileUploadBase64Img(String fileUrl,String fileName,byte [] fileArr,Integer mark) throws IOException{
		String fileType = fileName.substring(fileName.lastIndexOf(".")+1);
		String formatYear=new SimpleDateFormat("yyyy").format(new Date());
		String formatDate=new SimpleDateFormat("MMdd").format(new Date());
		String formatTime=new SimpleDateFormat("HHmmss").format(new Date());
		fileName=formatTime+getRandom()+"."+fileType;
		String severUrl="upload"+File.separator+"image"+File.separator+formatYear+File.separator+formatDate+File.separator+fileName;//服务器的相对地址
		String localUrl=fileUrl+severUrl;//保存在计算机上的地址
		File file =new File(localUrl);
		if(!file.exists()){
			if(!file.getParentFile().exists()){
				file.getParentFile().mkdirs();
			}
			file.createNewFile();
		}
		FileCopyUtils.copy(fileArr, file);
		//compressImage(new FileInputStream(file), 0, 0,localUrl);
		/**添加水印*/
		if (mark!=null) {
			markImage(fileUrl, localUrl);
		}
		return  "upload/image/"+formatYear+"/"+formatDate+"/"+fileName;
	}
	
	/**
	 * 解析base64图片
	 * @param request
	 * @param imgBytes
	 * @param mark
	 * @return
	 */
	public static  String uploadBase64Img(HttpServletRequest request,String imgBytes,Integer mark){
		if (imgBytes.contains("data:image/jpeg;base64,")) {
			imgBytes=imgBytes.substring("data:image/jpeg;base64,".length(), imgBytes.length());
		}
		if (imgBytes.contains("data:image/png;base64,")) {
			imgBytes=imgBytes.substring("data:image/png;base64,".length(), imgBytes.length());
		}
		BASE64Decoder decoder = new BASE64Decoder();  
		try {
			 byte[] b = decoder.decodeBuffer(imgBytes);
			 for(int i=0;i<b.length;++i) {  
	             if(b[i]<0){
	            	 //调整异常数据  
	                 b[i]+=256;  
	             }  
	         } 
			 String returnUrlStr=fileUploadBase64Img(request.getSession().getServletContext().getRealPath("/"), "x.png", b,mark);
			 String requestUrl=request.getRequestURL().toString();
			 requestUrl=requestUrl.substring(0, requestUrl.lastIndexOf("/"));
			 if (requestUrl.contains("userGetJsonInfo")) {
				 requestUrl=requestUrl.substring(0, requestUrl.lastIndexOf("/"));
			 }
			 String url=requestUrl+"/"+returnUrlStr;
			 System.out.println(url);
			 return url;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		return null;
	}
	
	/**
	 * 压缩图片 根据宽高
	 * @param fileStream   文件流
	 * @param width		      目的宽度
	 * @param height       目的高度
	 * @param comRate      压缩的宽高比例
	 * @param saveUrl      保存的路径
	 * @throws IOException
	 */
	public static void compressImage(InputStream fileStream,Integer width,Integer height,String saveUrl) throws IOException{
		ImageCompressUtil2 imgCom=new ImageCompressUtil2(fileStream, saveUrl);
		imgCom.resizeFix(width, height);  
	}
	
	/**
	 * 压缩图片  压缩大小
	 * @param fileStream   文件流
	 * @param saveUrl      保存的路径
	 * @throws IOException
	 */
	public static void compressImage(InputStream fileStream,String saveUrl) throws IOException{
		Image srcFile = ImageIO.read(fileStream);
		// SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢  
        BufferedImage image = new BufferedImage(srcFile.getHeight(null), srcFile.getHeight(null),BufferedImage.TYPE_INT_RGB );   
        image.getGraphics().drawImage(srcFile, 0, 0, srcFile.getHeight(null), srcFile.getHeight(null), null); // 绘制缩小后的图  
        File destFile = new File(saveUrl);  
        FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流  
        // 可以正常实现bmp、png、gif转jpg  
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
        encoder.encode(image); // JPEG编码  
        out.close();  
	}
	
	/**
	 * 压缩图片
	 * @param fileStream   文件流
	 * @param width		      目的宽度
	 * @param height       目的高度
	 * @param comRate      压缩的宽高比例
	 * @param saveUrl      保存的路径
	 * @throws IOException
	 */
	public static void compressImage(InputStream fileStream,Integer width,Integer height,Integer comRate,String saveUrl) throws IOException{
		Image srcFile = ImageIO.read(fileStream);
		if (comRate!=null&&comRate!=0) {
			if (srcFile.getWidth(null)>600) {
				 width = srcFile.getWidth(null)/comRate;  
			     height =srcFile.getHeight(null)/comRate;  
			}
		}else if (width==null&&height==null){
			width=150;
			height=150;
		}
		BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);
		FileOutputStream out = new FileOutputStream(saveUrl);
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
		/** 压缩质量 */
		jep.setQuality(1f, true);
		encoder.encode(tag, jep);
		out.close();
	}
	
	/**
	 * 解析base64图片
	 * @param imgBytes
	 * @return
	 */
	public static  String uploadBase64Img(String fileUrl,String imgBytes,Integer mark){
		if (imgBytes.contains("data:image/jpeg;base64,")) {
			imgBytes=imgBytes.substring("data:image/jpeg;base64,".length(), imgBytes.length());
		}
		if (imgBytes.contains("data:image/png;base64,")) {
			imgBytes=imgBytes.substring("data:image/png;base64,".length(), imgBytes.length());
		}
		BASE64Decoder decoder = new BASE64Decoder();  
		try {
			 byte[] b = decoder.decodeBuffer(imgBytes);
			 for(int i=0;i<b.length;++i) {  
	             if(b[i]<0){
	            	 //调整异常数据  
	                 b[i]+=256;  
	             }  
	         } 
			 return fileUploadBase64Img(fileUrl, "x.png", b,mark);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		return null;
	}
	
	/**
	 * 添加水印
	 * @param fileUrl
	 * @param localUrl
	 */
	public static  void markImage(String fileUrl,String localUrl){
		String iconPath=fileUrl+"bg"+File.separator+"image"+File.separator+"shuiyinlogo.png";//水印图片地址   工程路径/bg/image/shuiyinlogo.png
		String comIconPath=fileUrl+"bg"+File.separator+"image"+File.separator+new Date().getTime()+"comshuiyinlogo.png"; 
		ImageRemarkUtil.rateMarkImageByIcon(iconPath, comIconPath,localUrl, localUrl,null);
		//ImageRemarkUtil.pressImage(iconPath, localUrl, 4, 0.7f);
	}
	
	/**
	 * 创建文件路径
	 * @param fileUrl
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public static String getFileUploadUrl(HttpServletRequest request,String fileName) throws IOException{
		String fileUrl=request.getSession().getServletContext().getRealPath("/");
		String formatYear=new SimpleDateFormat("yyyy").format(new Date());
		String formatDate=new SimpleDateFormat("MMdd").format(new Date());
		String formatTime=new SimpleDateFormat("HHmmss").format(new Date());
		fileName=formatTime+getRandom()+"/"+fileName;
		String url="upload/"+formatYear+"/"+formatDate+"/"+fileName;
		File file =new File(fileUrl+url);
		if(!file.exists()){
			if(!file.getParentFile().exists()){
				file.getParentFile().mkdirs();
			}
		}
		
		return fileUrl+url;
	}
	
	/**
	 * 创建用户文件路径
	 * @param request
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public static String getUserFileUploadUrl(HttpServletRequest request,Long userId,String fileName) throws IOException{
		String fileUrl=request.getSession().getServletContext().getRealPath("/");
		String url="upload/userqr/"+userId+"/"+fileName;
		File file =new File(fileUrl+url);
		if(!file.exists()){
			if(!file.getParentFile().exists()){
				file.getParentFile().mkdirs();
			}
		}
		return fileUrl+url;
	}
	
	/**
	 * 获取文件工程路径
	 * @param request
	 * @param url
	 * @return
	 */
	public  static String getProjectFileUrl(HttpServletRequest request,String url){
		String requestUrl=request.getRequestURL().toString();
		String fileUrl=request.getSession().getServletContext().getRealPath("/");
		requestUrl=requestUrl.substring(0, requestUrl.lastIndexOf("/"));
		url=requestUrl+"/"+url.substring(fileUrl.length());
		if(url.contains("app/userGetInfo/")){
			url=url.replaceAll("app/userGetInfo/", "");
		}
		return url;
	}
	
	/**
	 * 获取工程路径
	 * @param request
	 * @param fileName
	 * @return
	 */
	public static String getUploadUrl(HttpServletRequest request,String fileName){
		String url=fileName;
		File file =new File(request.getSession().getServletContext().getRealPath("/")+File.separator+url);
		if(!file.exists()){
			if(!file.getParentFile().exists()){
				file.getParentFile().mkdirs();
			}
		}
		return file.getPath();
	}
	
	/**
	 * 获取服务器路径
	 * @param request
	 * @return
	 */
	public static String  getRequestUrl(HttpServletRequest request){
		String requestUrl=request.getRequestURL().toString();
		requestUrl=requestUrl.substring(0, requestUrl.lastIndexOf("/"));
		return requestUrl;
	}
	
	/**
	 * 多图上传
	 * @param request
	 * @param file
	 * @param width
	 * @param height
	 * @param comRate
	 * @param mark
	 * @return
	 */
	public static String getFilesImage(HttpServletRequest request, MultipartFile[] file,Integer width,Integer height,Integer comRate,Integer mark) {
		//文件处理
		try {
			String requestUrl=request.getRequestURL().toString();
			requestUrl=requestUrl.substring(0, requestUrl.lastIndexOf("/"));
			if (requestUrl.contains("userGetJsonInfo")) {
				requestUrl=requestUrl.replaceAll("/userGetJsonInfo", "");
			}
			String url="";
	        for (MultipartFile mf : file) {  
	            if(!mf.isEmpty()){  
	            	String returnUrlStr=imgUpload(request.getSession().getServletContext().getRealPath("/"), mf.getOriginalFilename(), mf.getInputStream(), width, height,comRate,mark);
					url+=requestUrl+"/"+returnUrlStr+",";
	            }
	        }
	        if (url.length()>0) {
	        	url=url.substring(0, url.length()-1);
			}
	        LogUtil.Info(url);
	        return url;
		} catch (Exception e) {
			LogUtil.Info("图片上传出错："+e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
	
	
	public static String getFileImage(HttpServletRequest request) {
		//文件处理
		try {
		    if (request instanceof MultipartHttpServletRequest) {
		    	String requestUrl=request.getRequestURL().toString();
				requestUrl=requestUrl.substring(0, requestUrl.lastIndexOf("/"));
				if (requestUrl.contains("userGetJsonInfo")) {
					requestUrl=requestUrl.replaceAll("/userGetJsonInfo", "");
				}
				String url="";
		        // process
		    	MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;             // 获得文件：        
		    	List<MultipartFile> files = multipartRequest.getFiles("info_file");
		    	for (MultipartFile mf : files) {
		    		if(!mf.isEmpty()){  
		    			String returnUrlStr=imgUpload(request.getSession().getServletContext().getRealPath("/"), mf.getOriginalFilename(), mf.getInputStream(), null, null,null,null);
						url+=requestUrl+"/"+returnUrlStr+",";
		    		} 
		    	}
		    	 LogUtil.Info(url.toString());
		    	 return url;
		    }
		} catch (Exception e) {
			 LogUtil.Info("图片上传出错："+e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 上传文件类型匹配
	 * @param fileName
	 * @return
	 */
	private static boolean checkFileType(String fileName){
		if (fileName.contains(".jpg")||fileName.contains(".JPG")||fileName.contains(".png")||fileName.contains(".PNG")||fileName.contains(".BMP")||fileName.contains(".bmp")) {
			return true;
		}
		return false;
	}
	
	
	
    /**
     * 得到amr的时长
     * 
     * @param file
     * @return
     * @throws IOException
     */
    public static long getAmrDuration(File file) throws IOException {
        long duration = -1;
        int[] packedSize = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
        RandomAccessFile randomAccessFile = null;
        try {
            randomAccessFile = new RandomAccessFile(file, "rw");
            long length = file.length();//文件的长度
            int pos = 6;//设置初始位置
            int frameCount = 0;//初始帧数
            int packedPos = -1;
            /////////////////////////////////////////////////////
            byte[] datas = new byte[1];//初始数据值
            while (pos <= length) {
                randomAccessFile.seek(pos);
                if (randomAccessFile.read(datas, 0, 1) != 1) {
                    duration = length > 0 ? ((length - 6) / 650) : 0;
                    break;
                }
                packedPos = (datas[0] >> 3) & 0x0F;
                pos += packedSize[packedPos] + 1;
                frameCount++;
            }
            /////////////////////////////////////////////////////
            duration += frameCount * 20;//帧数*20
        } finally {
            if (randomAccessFile != null) {
                randomAccessFile.close();
            }
        }
        return duration;
    }
	
    
    /**
    * 将网络图片转成Base64码，此方法可以解决解码后图片显示不完整的问题
    * @param imgURL图片地址。
    * 例如：http://***.com/271025191524034.jpg
    * @return
    */
    public static String HttpImg2Base64(String imgURL) {
    	ByteArrayOutputStream outPut = new ByteArrayOutputStream();
        byte[] data = new byte[1024];
        try {  
            // 创建URL  
            URL url = new URL(imgURL);  
            // 创建链接  
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
            conn.setRequestMethod("GET");  
            conn.setConnectTimeout(10 * 1000);  

            if(conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
            return "fail";//连接失败/链接失效/图片不存在
            }
            InputStream inStream = conn.getInputStream();  
            int len = -1;
            while ((len = inStream.read(data)) != -1) {
            outPut.write(data, 0, len);
            }
            inStream.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        // 对字节数组Base64编码  
        BASE64Encoder encoder = new BASE64Encoder(); 
        return encoder.encode(outPut.toByteArray()); 
    }
	
	public  static int getRandom(){
		Random random=new Random();
		return random.nextInt(99999);
	}
	
	public static void main(String[] args) {
		System.out.println(getRandom());
	}
}
